package com.app.micaihu.view.army.activity

import android.content.Intent
import android.os.Bundle
import com.app.base.BaseMvpActivity
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.bean.infor.AttentionStautsBean
import com.app.micaihu.bean.news.ShareBean
import com.app.micaihu.configure.Config
import com.app.micaihu.view.army.adapter.ArmyMemberListAdapter
import com.app.micaihu.view.army.contract.ArmyMemberContract
import com.app.micaihu.view.army.dialog.ArmyMemberDelDialog
import com.app.micaihu.view.army.dialog.ArmyMemberMenuDialog
import com.app.micaihu.view.army.dialog.ArmyMemberShareDialog
import com.app.micaihu.view.army.header.ArmyMemberHeader
import com.app.micaihu.view.army.presenter.ArmyMemberPresenter
import com.app.micaihu.view.bean.ArmyMember
import com.app.micaihu.view.main.view.WrapContentLinearLayoutManager
import com.app.micaihu.view.user.userinfo.activity.MyHomepageDynamicActivity
import com.app.utils.biz.Biz
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.umeng.socialize.UMShareAPI
import kotlinx.android.synthetic.main.army_member_list_activity.*

/**
 * 军团成员列表
 */
class ArmyMemberListActivity : BaseMvpActivity<ArmyMemberContract.View, ArmyMemberPresenter>(), ArmyMemberContract.View {

    override fun getLayoutId() = R.layout.army_member_list_activity

    override fun getBundle(bundle: Bundle) {
        gid = bundle.getString(Config.Extra.id, "")
    }

    override fun getPresenter() = ArmyMemberPresenter(gid)

    private var gid = ""
    private var isDel = false
    private var followPosition = 0
    private var haveDelAuthority = false

    private var toUid: String = ""
    private val uid: String by lazy {
        Biz.instance.uid()
    }
    private val armyMemberMenuDialog: ArmyMemberMenuDialog by lazy {
        ArmyMemberMenuDialog()
    }
    private val armyMemberShareDialog: ArmyMemberShareDialog by lazy {
        ArmyMemberShareDialog()
    }
    private val armyMemberDelDialog: ArmyMemberDelDialog by lazy {
        ArmyMemberDelDialog()
    }

    private val armyMemberListAdapter: ArmyMemberListAdapter by lazy {
        val armyMemberListAdapter = ArmyMemberListAdapter()
        rvMember.layoutManager = WrapContentLinearLayoutManager(thisContext)
        rvMember.adapter = armyMemberListAdapter
        armyMemberListAdapter
    }

    private val armyMemberHeader: ArmyMemberHeader by lazy {
        ArmyMemberHeader(thisContext, gid)
    }

    override fun setData() {
        BarUtils.addMarginTopEqualStatusBarHeight(rlTitle)
        mPresenter?.armyMemberList()
        mPresenter?.share()
    }

    override fun addListeners() {
        tvBack.setOnClickListener {
            if (isDel) {
                isDel = false
                setTitleText(isDel)
                armyMemberListAdapter.setShowSelect(false)
            } else {
                finish()
            }
        }
        tvRight.setOnClickListener {
            if (isDel) {
                toUid = armyMemberListAdapter.getSelectUid()
                if (toUid.isEmpty()) {
                    ToastUtils.showShort("至少选择一个成员")
                } else {
                    armyMemberDelDialog.show(supportFragmentManager)
                }
            } else {
                val bundle = Bundle()
                bundle.putBoolean(Config.Extra.showDel, haveDelAuthority)
                bundle.putBoolean(Config.Extra.showInviteMicaihu, haveDelAuthority)
                armyMemberMenuDialog.arguments = bundle
                armyMemberMenuDialog.show(supportFragmentManager)
            }

        }
        armyMemberListAdapter.setOnItemClickListener { adapter, view, position ->
            val item = armyMemberListAdapter.getItem(position)
            if (isDel) {
                item.select = !item.select
                armyMemberListAdapter.notifyItemChanged(position + armyMemberListAdapter.headerLayoutCount)
            } else {
                MyHomepageDynamicActivity.goToUserHomePage(thisActivity, item.uid)
            }
        }
        armyMemberMenuDialog.setOnMenuClickListener(object : ArmyMemberMenuDialog.OnMenuClickListener {
            override fun onInvitationClick() {
                armyMemberMenuDialog.dismiss()
                val bundle = Bundle()
                bundle.putString(Config.Extra.id, gid)
                enterPager(ArmyInviteMemberListActivity::class.java, bundle)
            }

            override fun onShareClick() {
                armyMemberMenuDialog.dismiss()
                armyMemberShareDialog.show(supportFragmentManager)
            }

            override fun onDelClick() {
                armyMemberMenuDialog.dismiss()
                if (armyMemberListAdapter.data.size > 0) {
                    armyMemberListAdapter.setShowSelect(true)
                    isDel = true
                    setTitleText(isDel)
                } else {
                    ToastUtils.showShort("没有可以删除的成员")
                }
            }
        })
        armyMemberDelDialog.setOnDelMemberClickListener(object : ArmyMemberDelDialog.OnDelMemberClickListener {
            override fun onDelMemberClick() {
                armyMemberDelDialog.dismiss()
                mPresenter?.deleteMember(gid, toUid)
            }

        })
    }

    fun setTitleText(isDel: Boolean) {
        this.isDel = isDel
        if (isDel) {
            tvBack.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
            tvBack.text = "取消"

            tvRight.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
            tvRight.text = "删除"

        } else {
            tvBack.setCompoundDrawablesWithIntrinsicBounds(R.drawable.common_back_selector, 0, 0, 0)
            tvBack.text = ""

            tvRight.setCompoundDrawablesWithIntrinsicBounds(R.drawable.detail_menu_black_selector, 0, 0, 0)
            tvRight.text = ""
        }
    }

    override fun handleArmyMemberList(result: BaseBean<ArmyMember?>?) {
        if (result == null) {
            armyMemberListAdapter.setEmptyView(errorView)
        } else {
            if (result.isSuccess()) {
                result.data?.let { data ->
                    for (item in data.adminList) {
                        if (uid == item.uid) {
                            haveDelAuthority = true
                            break
                        }
                    }
                    data.memberList?.let {
                        armyMemberHeader.setMemberSize(it.size)
                    }
                    armyMemberHeader.setHaveDelAuthority(haveDelAuthority)
                    armyMemberHeader.setList(data.adminList)
                    if (!armyMemberListAdapter.hasHeaderLayout()) {
                        armyMemberListAdapter.addHeaderView(armyMemberHeader)
                    }
                    armyMemberListAdapter.setList(data.memberList)
                }
            } else {
                ToastUtils.showShort(result.msg)
            }
        }
    }

    override fun handleFollow(result: BaseBean<AttentionStautsBean?>?) {
        if (result == null) {
            ToastUtils.showShort(getString(R.string.net_error))
        } else {
            if (result.isSuccess()) {
                result.data?.let {
                    armyMemberListAdapter.getItem(followPosition).attentionStatus = it.status
                    armyMemberListAdapter.notifyItemChanged(followPosition + armyMemberListAdapter.headerLayoutCount)
                }
            }
            ToastUtils.showShort(result.msg)
        }
    }

    override fun handleDeleteMember(result: BaseBean<Any?>?) {
        if (result == null) {
            ToastUtils.showShort(getString(R.string.net_error))
        } else {
            if (result.isSuccess()) {
                LiveEventBus.get(Config.EventBus.delArmyMemberSuccess).post(true)
                val dataList = armyMemberListAdapter.data
                val toUidList = toUid.split(",")
                for (item in toUidList) {
                    val iterator = dataList.iterator()
                    while (iterator.hasNext()) {
                        val dataItem = iterator.next()
                        if (dataItem.uid == item) {
                            iterator.remove()
                        }
                    }
                }
                armyMemberListAdapter.setList(dataList)
            }
            ToastUtils.showShort(result.msg)
        }
    }

    override fun handleShare(result: BaseBean<ShareBean?>?) {
        if (result != null) {
            if (result.isSuccess()) {
                result.data?.let {
                    val bundle = Bundle()
                    bundle.putSerializable(Config.Extra.share, it)
                    armyMemberShareDialog.arguments = bundle
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data)
    }
}